#include <stdio.h>
#include <string.h>
#include "include/fncts.h"
#include "include/structs.h"
#include "include/events.h"
char *get_current_dir_name(void);
int n_Lines(char sub_path[]) //YOUSSEF
{
	char ch;
	int lines=0;
	char dir_path[1024];
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dir_path,"%s",get_current_dir_name());
        strcat(dir_path,sub_path);
        FILE *file=fopen(dir_path,"r+");
        if(file==NULL){printf("Error File");return -1;}
	while(!feof(file))
	{
  	ch = fgetc(file);
  	if(ch == '\n')
  		{
    			lines++;
  		}
	}

	return lines;
}
void ReadEvent(char sub_path[],int lines)//YOUSSEF
{
	int i,j,k=0;
	event event[lines],parc[lines];
	char dir_path[1024],buff[512];
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dir_path,"%s",get_current_dir_name());
        strcat(dir_path,sub_path);
	FILE *eventfile=fopen(dir_path,"r+");
	if(eventfile==NULL){printf("Error File");return;}
	rewind(eventfile);
	for(i=0;i<lines;i++)
	{
	  fgets(buff,512,eventfile);
	  sscanf(buff,"%s %d %s",event[i].time,&(event[i].id),event[i].capteur);
	  //printf("inFile : %s %d %s \n",event[i].time,event[i].id,event[i].capteur);
	  //printf("%s",buff);
	}
	k=0;
	for(i=0;i<lines;i++)
	{
	  if(event[i].id != -1)
	  {
		parc[k] = event[i];k++;
		//printf("%s %d %s \n",parc[k-1].time,parc[k-1].id,parc[k-1].capteur);
		for(j=i+1;j<lines;j++)
		{
			if((event[i].id == event[j].id) )
			{
				parc[k] = event[j];k++;
				event[j].id = -1 ;
				//printf("%s %d %s \n",parc[k-1].time,parc[k-1].id,parc[k-1].capteur);
			}
		}
	  }
	}
	k=Parccmp(parc,lines);
	//printf("%d",k);
	fclose(eventfile);
}
int Parccmp(event parc[],int lines)//OUWAYES
{
	int i,k=0,id_prev,ebola=0,pout=0,p_test=0,pasmed=0;
	staffmed med;
	char Parc_correct[][5]={"E01","C01","S01","L01","C02","L02","V01","I01","D01","B01"};
	id_prev = parc[0].id;
	for(i=0;i<lines;i++)
	{
		if( parc[i].id != id_prev )
		{
			k     = 0;
			ebola = 0;
			pout  = 0;
			p_test= 0;
			pasmed= 0;
			read_med_file("/tmp/dbmed",parc[i].id,&med);
		}
		if(read_med_file("/tmp/dbmed",parc[i].id,&med)!=1 && pasmed!=1)
		{
                        printf("--------INFRACTION-DETECTE------- \n");
			printf("%d : pas du staff medical ! \n",parc[i].id);
                        printf("--------INFRACTION-DETECTE------- \n");
			ebola = 1;
			pout  = 1;
			p_test= 1;
			pasmed= 1;
		}
		if( (strcmp(parc[i].capteur,"Ebola")==0) && (ebola!=1))
		{
                        printf("--------INFRACTION-DETECTE------- \n");
			printf("ID: %d | Nom: %s | Prenom: %s | CIN: %ld \n",med.id,med.nom,med.prenom,med.cin);
			printf("%d : infecter par Ebola \n",parc[i].id);
                      	printf("--------INFRACTION-DETECTE------- \n");
			ebola = 1;
		}
		if( (strcmp(parc[i].capteur,"Pout")==0) && (pout!=1))
		{	
                        printf("--------INFRACTION-DETECTE------- \n");
			printf("ID: %d | Nom: %s | Prenom: %s | CIN: %ld \n",med.id,med.nom,med.prenom,med.cin);
			printf("%d : Depassement de la barrier \n",parc[i].id);
                        printf("--------INFRACTION-DETECTE------- \n");
			pout  = 1;
		}
		if((pout != 1) && (ebola != 1) && (p_test != 1))
		{
			if(strcmp(parc[i].capteur,Parc_correct[k])!=0 && (k!=9))
			{	
				printf("--------INFRACTION-DETECTE------- \n");
				printf("ID: %d | Nom: %s | Prenom: %s | CIN: %ld \n",med.id,med.nom,med.prenom,med.cin);
				printf("%d : Capteur %s activer hors-parcours ! \n",parc[i].id,parc[i].capteur);
                                printf("--------INFRACTION-DETECTE------- \n");
				p_test = 1;
			}
			if( (k==9) && ( (strcmp(parc[i].capteur,"B01")!=0) || (strcmp(parc[i].capteur,"M01")!=0)) )
			{	
                                printf("--------INFRACTION-DETECTE------- \n");
				printf("ID: %d | Nom: %s | Prenom: %s | CIN: %ld \n",med.id,med.nom,med.prenom,med.cin);
				printf("%d : Capteur %s activer hors-parcours ! \n",parc[i].id,parc[i].capteur);
                                printf("--------INFRACTION-DETECTE------- \n");
                                p_test = 1;
			}
		}
		k++;
		id_prev=parc[i].id;
	}
	return 0;
}
